package com.letoken.platform.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.AdminAppService;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.constant.LetokenUserConstant;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.req.user.*;
import com.letoken.platform.repo.admin.res.user.*;
import com.letoken.platform.repo.app.dao.AppConsumerWeightingMapper;
import com.letoken.platform.repo.app.dao.AppUserTeamMapper;
import com.letoken.platform.repo.app.po.AppUserTeam;
import com.letoken.platform.repo.utlis.PageDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author letokenlabs
 * @date 2022/11/23$
 */
@Slf4j
@RestController
@RequestMapping("/app")
@Api(tags = "app相关接口")
public class AdminAppController {

    @Autowired
    private AdminAppService adminAppService;

    @Autowired
    AppConsumerWeightingMapper appConsumerWeightingMapper;

    @Resource
    private AppUserTeamMapper appUserTeamMapper;

    @Permission(uri = "/app/user/list",qx = "/user/index")
    @Trim
    @PostMapping("/user/list")
    @ApiOperation(value = "app用户列表", notes = "app用户列表")
    public ApiResponse<PageData<QueryAppUserListRes>> queryUserList(@RequestBody @Validated QueryAppUserListReq req) {
        return ApiResponse.create(adminAppService.queryUserList(req));
    }


    @Permission(uri = "/app/user/team/update",qx = "/app/user/team/update")
    @Trim
    @PostMapping("/user/team/update")
    @ApiOperation(value = "app用户列表", notes = "app用户列表")
    public ApiResponse<PageData<QueryAppUserListRes>> appUserTeamUpdate(@RequestBody @Validated AppUserTeamUpdateReq req) {
        AppUserTeam appUserTeam = new AppUserTeam();
        appUserTeam.setUserId(req.getUserId());
        appUserTeam.setLevel(req.getLevel());
        appUserTeam.setUsername(req.getUsername());
        appUserTeam.setCommunityAddress(req.getCommunityAddress());
        appUserTeamMapper.updateById(appUserTeam);
        return ApiResponse.create();
    }




    @Permission(uri = "/app/user/isLocked",qx = "/app/user/isLocked")
    @PostMapping("/user/isLocked")
    @ApiOperation(value = "是否锁定用户 true：锁定 false：取消锁定", notes = "是否锁定用户 true：锁定 false：取消锁定")
    public ApiResponse<?> isLockedUser(@RequestBody @Validated IsLockedUserReq req) {
        adminAppService.isLockedUser(req);
        return ApiResponse.create();
    }

    @Permission(uri = "/app/user/count",qx = "/app/user/count")
    @GetMapping("/user/count")
    @ApiOperation(value = "统计用户数量", notes = "统计用户数量")
    public ApiResponse<QueryUserCountByDateRes> queryUserCountByDate() {
        return ApiResponse.create(adminAppService.queryUserCountByDate());
    }

    @Permission(uri = "/app/user/LineChart",qx = "/app/user/LineChart")
    @PostMapping("/user/LineChart")
    @ApiOperation(value = "用户记录折线图", notes = "用户记录折线图")
    public ApiResponse<List<QueryLineChartRes>> queryUserLineChart(@RequestBody @Validated QueryLineChartReq req) {
        return ApiResponse.create(adminAppService.queryUserLineChart(req));
    }

    @Permission( uri = "/app/user/kol/list",qx = "/user/kol")
    @Trim
    @PostMapping("/user/kol/list")
    @ApiOperation(value = "KOL列表", notes = "kol列表")
    public ApiResponse<PageData<QueryAppKolListRes>> queryUserTeamList(@RequestBody @Validated QueryAppKolListReq req) {
        return ApiResponse.create(adminAppService.queryUserTeamList(req));
    }

    @Permission(uri = "/app/user/assets/list",qx = "/user/assets")
    @Trim
    @PostMapping("/user/assets/list")
    @ApiOperation(value = "app用户资产", notes = "app用户资产")
    public ApiResponse<PageData<QueryAppAssetsListRes>> queryUserAssetsList(@RequestBody @Validated QueryAppAssetsListReq req) {
        return ApiResponse.create(adminAppService.queryUserAssetsList(req));
    }

    @Permission(uri = "/app/user/assets/details/list",qx = "/app/user/assets/details/list")
    @GetMapping("/user/assets/details/list/{userId}")
    @ApiOperation(value = "app用户资产明细列表", notes = "app用户资产明细列表")
    public ApiResponse<PageData<QueryAssetsHistoryRes>> queryUserAssetsDetailsList(@PathVariable("userId") Integer userId,
                                                                             @RequestParam(value = "currency", required = false) String currency,
                                                                             @RequestParam("page") Integer page,
                                                                             @RequestParam("size") Integer size) {

        return ApiResponse.create(adminAppService.queryUserAssetsDetailsList(userId,currency,  page, size));
    }


    @Permission(uri = "/app/user/token/available_balance",qx = "/app/user/token/available_balance")
    @GetMapping("/user/token/available_balance/{userId}")
    @ApiOperation(value = "用户币的可用余额", notes = "用户币的可用余额")
    public ApiResponse<List<QueryAppUserTokenAilableBalanceRes>> queryUserAvailableBalance(@PathVariable("userId") Integer userId) {
        return ApiResponse.create(adminAppService.queryUserAvailableBalance(userId));
    }

    @Permission(uri = "/app/user/assets/sum",qx = "/user/assets/sum")
    @Trim
    @GetMapping("/user/assets/sum")
    @ApiOperation(value = "统计用户总资产", notes = "统计用户总资产")
    public ApiResponse<?> queryUserAssetsSum() {
        return ApiResponse.create(adminAppService.queryUserAssetsSum());
    }


    @Permission( uri = "/app/user/teamAchievement/list",qx = "/user/team")
    @Trim
    @PostMapping("/user/teamAchievement/list")
    @ApiOperation(value = "团队业绩", notes = "团队业绩")
    public ApiResponse<List<QueryTeamAchievementRes>> queryTeamAchievementList(@RequestBody @Validated QueryTeamAchievementReq req) {
        return ApiResponse.create(adminAppService.queryTeamAchievementList(req));
    }


    @Permission(uri = "/app/wallet/deposit/count",qx = "/app/wallet/deposit/count")
    @GetMapping("/wallet/deposit/count")
    @ApiOperation(value = "统计充值记录", notes = "统计充值记录")
    public ApiResponse<QueryAppRechargeCountRes> queryRechargeCount() {
        return ApiResponse.create(adminAppService.queryRechargeCount());
    }

    @Permission( uri = "/app/wallet/deposit/list",qx = "/platformFunds/rechargeRecord")
    @PostMapping("/wallet/deposit/list")
    @ApiOperation(value = "充值记录", notes = "充值记录")
    public ApiResponse<PageData<QueryAppDepositListRes>> queryDepositList(@RequestBody @Validated QueryAppDepositListReq req) {
        return ApiResponse.create(adminAppService.queryDepositList(req));
    }

    @Permission(uri = "/app/wallet/deposit/LineChart",qx = "/app/wallet/deposit/LineChart")
    @PostMapping("/wallet/deposit/LineChart")
    @ApiOperation(value = "充值记录折线图", notes = "充值记录折线图")
    public ApiResponse<List<QueryLineChartRes>> queryDepositLineChart(@RequestBody @Validated QueryLineChartReq req) {
        return ApiResponse.create(adminAppService.queryDepositLineChart(req));
    }

    @Permission(uri = "/app/wallet/withdraw/count",qx = "/app/wallet/withdraw/count")
    @GetMapping("/wallet/withdraw/count")
    @ApiOperation(value = "统计提现记录", notes = "统计提现记录")
    public ApiResponse<QueryWithdrawCountRes> queryWithdrawCount() {
        return ApiResponse.create(adminAppService.queryWithdrawCount());
    }

    @Permission(uri = "/app/wallet/withdraw/list",qx = "/platformFunds/withdrawalsRecord")
    @Trim
    @PostMapping("/wallet/withdraw/list")
    @ApiOperation(value = "提现记录", notes = "提现记录")
    public ApiResponse<PageData<QueryAppWithdrawListRes>> queryWithdrawList(@RequestBody @Validated QueryAppWithdrawListReq req) {

        return ApiResponse.create(adminAppService.queryWithdrawList(req));
    }

    @Permission(uri = "/app/wallet/withdraw/LineChart",qx = "/app/wallet/withdraw/LineChart")
    @PostMapping("/wallet/withdraw/LineChart")
    @ApiOperation(value = "提现记录折线图", notes = "提现记录折线图")
    public ApiResponse<List<QueryLineChartRes>> queryWithdrawLineChart(@RequestBody @Validated QueryLineChartReq req) {
        return ApiResponse.create(adminAppService.queryWithdrawLineChart(req));
    }

    @Permission(uri = "/app/wallet/withdraw/auditAgainYes",qx = "/app/wallet/withdraw/auditAgainYes")
    @Trim
    @PostMapping("/wallet/withdraw/auditAgainYes")
    @ApiOperation(value = "提现记录-提现审核重新通过", notes = "提现审核重新通过")
    public ApiResponse<EditWithdrawYesRes> auditAgainYes(@RequestBody @Validated EditWithdrawYesReq req) {
        // adminAppService.auditAgainYes(req)
        return ApiResponse.create();
    }

    @Permission(uri = "/app/fn/withdraw/audit/list",qx = "/platformFunds/withdrawalApplication")
    @Trim
    @PostMapping("/fn/withdraw/audit/list")
    @ApiOperation(value = "提现审核-查询", notes = "提现审核-查询")
    public ApiResponse<PageData<QueryAppWithdrawAuditListRes>> queryWithdrawAuditList(@RequestBody @Validated QueryAppWithdrawAuditListReq req) {
        return ApiResponse.create(adminAppService.queryWithdrawAuditList(req));
    }

    @Permission(uri = "/app/fn/withdraw/audit/yes",qx = "/app/fn/withdraw/audit/yes")
    @Trim
    @PostMapping("/fn/withdraw/audit/yes")
    @ApiOperation(value = "提现审核-通过", notes = "提现审核-通过")
    public ApiResponse<EditWithdrawYesRes> editWithdrawYes(@RequestBody @Validated EditWithdrawYesReq req) {
        //adminAppService.editWithdrawYes(req)
        return ApiResponse.create();
    }

    @Permission(uri = "/app/fn/withdraw/audit/no",qx="/app/fn/withdraw/audit/no")
    @Trim
    @PostMapping("/fn/withdraw/audit/no")
    @ApiOperation(value = "提现审核-拒绝", notes = "提现审核-拒绝")
    public ApiResponse<EditWithdrawNoRes> editWithdrawNo(@RequestBody @Validated EditWithdrawNoReq req) {
        // adminAppService.editWithdrawNo(req)
        return ApiResponse.create();
    }

    @Permission(uri = "/app/fn/withdraw/history",qx = "/app/fn/withdraw/history")
    @GetMapping("/fn/withdraw/history/{userId}")
    @ApiOperation(value = "用户资产信息", notes = "用户资产信息")
    public ApiResponse<QueryWithdrawHistoryRes> queryWithdrawHistory(@PathVariable("userId") Integer userId) {
        return ApiResponse.create(adminAppService.queryWithdrawHistory(userId));
    }


    @Permission(uri = "/app/quickswap/list",qx = "/flashManage/manageIndex")
    @Trim
    @PostMapping("/quickswap/list")
    @ApiOperation(value = "闪兑记录", notes = "闪兑记录")
    public ApiResponse<PageData<QueryQuickswapListRes>> queryQuickswapList(@RequestBody @Validated QueryQuickswapListReq req) {
        return ApiResponse.create(adminAppService.queryQuickswapList(req));
    }



    @Permission(uri = "/app/consumer/weighting",qx = "/app/consumer/weighting")
    @Trim
    @PostMapping("/consumer/weighting")
    public ApiResponse<PageData<ConsumerWeightingRes>> consumerWeighting(@RequestBody @Validated ConsumerWeightingReq req) {
        try {
            if (ObjectUtils.isNotEmpty(req.getLeId())){
                if (req.getLeId().startsWith(LetokenUserConstant.USER_ID_PREFIX)){
                    req.setUserId(Integer.valueOf(req.getLeId().replace(LetokenUserConstant.USER_ID_PREFIX,"")));
                }else {
                    req.setUserId(Integer.valueOf(req.getLeId()));
                }
            }
        } catch (NumberFormatException e) {
            log.error("leId格式不对",e);
            req.setUserId(null);
        }

        Page<ConsumerWeightingRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<ConsumerWeightingRes> QuickswapPage = appConsumerWeightingMapper.consumerWeighting(page, req);
        return ApiResponse.create(PageDataUtil.getPageData(QuickswapPage));
    }


    @Permission(uri = "/app/eco/history",qx = "/flashManage/manageIndex")
    @Trim
    @PostMapping("/eco/history")
    @ApiOperation(value = "用户返回值", notes = "闪兑记录")
    public ApiResponse<PageData<QueryQuickswapListRes>> ecoHistory(@RequestBody @Validated QueryAppEcoHistoryReq req) {
        return ApiResponse.create(adminAppService.queryUserEcoHistoryList(req.getUserId(), req.getPageReq().getPage(), req.getPageReq().getSize()));
    }


    @Permission(uri = "/app/ecoLevel/history",qx = "/app/ecoLevel/history")
    @Trim
    @PostMapping("/ecoLevel/history")
    public ApiResponse<PageData<UpLevelHistoryRes>> ecoLevelHistory(@RequestBody @Validated EcoLevelHistoryReq req) {
        try {
            if (ObjectUtils.isNotEmpty(req.getLeId())){
                if (req.getLeId().startsWith(LetokenUserConstant.USER_ID_PREFIX)){
                    req.setUserId(Integer.valueOf(req.getLeId().replace(LetokenUserConstant.USER_ID_PREFIX,"")));
                }else {
                    req.setUserId(Integer.valueOf(req.getLeId()));
                }
            }
        } catch (NumberFormatException e) {
            log.error("leId格式不对",e);
            req.setUserId(null);
        }

        return ApiResponse.create(adminAppService.ecoLevelHistory(req));
    }


    @Permission( uri = "/app/user/kol/teamDateList",qx = "/app/user/kol/teamDateList")
    @Trim
    @GetMapping("/user/kol/teamDateList")
    @ApiOperation(value = "团队人数", notes = "团队人数")
    public ApiResponse<List<QueryAppKolListRes>> teamList( @RequestParam("userId") Integer userId) {


        return ApiResponse.create(appUserTeamMapper.getTeamList(userId));
    }


}
